<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title></title>
    <script src="js/mui.min.js"></script>
    <link href="css/mui.min.css" rel="stylesheet"/>
	<link href="css/header.css" rel="stylesheet" />
	<link rel="stylesheet" type="text/css" href="custom_iconfont/iconfont.css"/>
</head>
<body>
	<header class="mui-bar mui-bar-nav title-bar">
		<h1 class="mui-title title-content"><b>糖果闲聊<span id="connectionStatus"></span></b></h1>
	</header>
	<nav class="mui-bar mui-bar-tab">
		<a class="mui-tab-item mui-active" tabIndex="0">
			<span class="mui-icon iconfont icon-liaotian"></span>
			<span class="mui-tab-label">聊天</span>
		</a>
		<a class="mui-tab-item" tabIndex="1">
			<span class="mui-icon iconfont icon-imedia_tongxunlu
"></span>
			<span class="mui-tab-label">通讯录</span>
		</a>
		<a class="mui-tab-item" tabIndex="2">
			<span class="mui-icon iconfont icon-faxian"></span>
			<span class="mui-tab-label">发现</span>
		</a>
		<a class="mui-tab-item" tabIndex="3">
			<span class="mui-icon iconfont icon-gerenzhongxin-zhong"></span>
			<span class="mui-tab-label">我</span>
		</a>
	</nav> 
	<script src="js/app.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript" charset="utf-8">
	  	mui.init();
		
		var candyChatArray = [
			{
				pageId: "chatList.html",
				pageUrl: "chatList.html"
			},
			{
				pageId: "addressBook.html",
				pageUrl: "addressBook.html"
			},
			{
				pageId: "descover.html",
				pageUrl: "descover.html"
			},
			{
				pageId: "me.html",
				pageUrl: "me.html"
			}
		];
		
		var candyChatStyle = {
			top: "44px",
			bottom: "50px"
		}
		
		mui.plusReady(function () {
			// 禁止返回到登录注册页面
			mui.back = function() {
				return false;
			}
			
			// 对网络连接进行事件监听
			netChangeListener();
			
			plus.navigator.setStatusBarStyle("light");
			plus.navigator.setStatusBarBackground("#C9394A");
			var indexWebview = plus.webview.currentWebview();
			// 向当前的主页webview中追加4张子页webview对象
			for(var i = 0; i < candyChatArray.length; i++) {
				var candyChatPage = plus.webview.create(candyChatArray[i].pageUrl, 
														candyChatArray[i].pageId, 
														candyChatStyle);
				// 隐藏子webview页面
				candyChatPage.hide();
				// 追加子页面到主页面中
				indexWebview.append(candyChatPage);
			}
			// 显示第一个子webview页面
			plus.webview.show(candyChatArray[0].pageId);
			
			// 批量绑定tab事件，展示不同的页面
			mui(".mui-bar-tab").on("tap", "a", function() {
				var tabIndex = this.getAttribute("tabIndex");
				// 显示点击的tab选项所对应的页面
				plus.webview.show(candyChatArray[tabIndex].pageId, "fade-in", 200);
				// 隐藏其他页面
				for(var i = 0; i < candyChatArray.length; i++) {
					if(i != tabIndex) {
						plus.webview.hide(candyChatArray[i].pageId, "fade-out", 200);
					}
				}
			});
			
			// 延时一秒钟，预加载子页面
			setTimeout("preloadChildWebview()", "1000");
		});
		
		function preloadChildWebview() {
			refreshWebViewById("chatList.html");
			refreshWebViewById("me.html");
		}
		
		function refreshWebViewById(wid) {
			var webview = plus.webview.getWebviewById(wid);
			mui.fire(webview, "refresh");
		}
		
		// 监听网络状态的变化
		function netChangeListener() {
			document.addEventListener("netchange", function() {
				var connectionTip = "";
				if(!app.isConnectNetWork()) {
					connectionTip = "（未连接网络）";
				}
				document.getElementById("connectionStatus").innerHTML = connectionTip;
			});
		}
	</script>
</body>
</html>